<?php
/**
 * Created by PhpStorm.
 * User: zhangfulai
 * Date: 2018/10/3
 * Time: 上午9:34
 */
namespace app\api\model;
use think\Db;
use think\Model;
class Carinfor extends Model{

    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    protected $updateTime = 'utime';
    protected $insert = ['status' => 0,'defaultcar'=>2];

    protected function getCtimeAttr($value){
        return date("Y-m-d H:i:s",$value);
    }
    protected function getUtimeAttr($value){
        return date("Y-m-d H:i:s",$value);
    }


    /*
    * Notes: 视图查询车辆信息
    * Create Date: 2018/10/3
    * Create Time: 下午5:05
    * Create by :张福来
    * orange_carinfouser : 视图
    */
    public function selectCarinfo($pageNum,$listNum,$w){
        $result["list"] = Db::name("carinfor")
                  ->where($w)
                  ->page($pageNum,$listNum)
                  ->order("ctime desc")
                  ->select();
        $result["total"] = Db::name("carinfor")
                  ->where($w)
                  ->count();
        return $result;
    }

    /**
     * Notes: 查询车辆信息
     * Create Date: 2018/10/2
     * Create by :lijie
     */
    public function carInfoByUser($w){
        $result = $this->where($w)->find();
        return $result;
    }
    /**
     * 查询车辆列表
     * @author 王涛
     * @DataTime 2018-08-09T17:16:16+0800
     * @param    [type]                   $w [description]
     * @return   [type]                      [description]
     */
    public function carsInfoByUser($w){
        $result = $this->where($w)->select();
        return $result;
    }
    /**
     * Notes: 修改车辆信息
     * Create Date: 2018/10/2
     * Create by :lijie
     */
    public function editCarInfo($w,$save){

        $result = $this->save($save,$w);
        return $result;
    }

    /**
     * Notes: 添加车辆信息
     * Create Date: 2018/10/2
     * Create by :lijie
     */
    public function addCarInfo($save){
        $result = $this->allowField(true)->save($save);
        if ($result) {
            return $this->getLastInsID();
        }else{
            return false;
        }
    }
    /**
     * 设置默认用户车辆
     * @author 王涛
     * @DataTime 2018-08-09T17:28:14+0800
     * @param    [type]                   $data [description]
     * @return   [type]                         [description]
     */
    public function defaultCar($data)
    {
        $up['defaultcar'] = 2;
        return $this->where($data)->update($up);
    }

    /**
     * 去除此用户的所有默认车辆 重写
     * @param $u_id
     * @author  zhenHong~
     */
    public function noDefaultCar($u_id)
    {
        $where = [
            'u_id' => $u_id,
            'defaultcar' => 2
        ];
        $this->where($where)->setField('defaultcar','1');
    }
    /**
     * 用户删除车辆
     * @author 王涛
     * @DataTime 2018-08-11T18:57:32+0800
     * @param    [type]                   $w [description]
     * @return   [type]                      [description]
     */
    // public function carDel($w)
    // {

    // }
}